package com.sooncode.project.focusservice.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sooncode.project.core.annotations.Lookup;
import com.sooncode.project.core.annotations.LookupModel;
import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.core.finder.Sort;
import com.sooncode.project.core.generic.BasicAddEvent;
import com.sooncode.project.core.generic.BasicModifyEvent;
import com.sooncode.project.focusservice._.base.BaseDomainModel;
import com.sooncode.project.focusservice._.base.Utils;
import com.sooncode.project.springcloudcommon.utils.CommonUtils;
import com.sooncode.project.springcloudcommon.utils.DateUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
@LookupModel
public class Member extends BaseDomainModel<Member> {
    private String orgId;
    @Lookup(fromModel = Organization.class,localField = "orgId",fromField = "name")
    private String orgName;
    private String code;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createDate;
    private String channelId;
    @Lookup(fromModel = Channel.class,localField = "channelId",fromField = "name")
    private String channel;
    private String member_no;
    private String studId;//原系统的 ID
    private String name;
    private String sex;
    private String birth;
    private String age;
    private String showAge;
    private String parent_name;
    private boolean feed;
    private String feeName;
    private double courseHours;
    private double usedHours;
    private double remainHours;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date feeDate;
    private double money;
    private String statusId;
    @Lookup(fromModel = MemberStatus.class,localField = "statusId",fromField = "name")
    private String member_status;
    private String phone;
    private String salesPerson;
    private String notes;
    private String courseFile;
    private long downloadCourse;
    private String padCode;
    private String classId;
    @Lookup(fromModel = Classes.class,localField = "classId",fromField = "name")
    private String className;
    @Lookup(fromModel = Classes.class,localField = "classId",fromField = "year")
    private String classYear;

    public double getRemainHours() {
        if(getCourseHours()>0)
            return getCourseHours()-getUsedHours();
        else return 0;
    }

    public void addDownloadCourse(){
        setDownloadCourse(downloadCourse++);
        causes(BasicModifyEvent.class,this);
    }

    @Override
    public void add(Map map){
        Organization org=new Finder<>(Organization.class).byId(map.get("orgId").toString());
        map.put("member_no",String.format("%07d",org.newSerialNum()));
        super.add(map);
    }

    public void add(Organization org){
        setMember_no(String.format("%07d",org.newSerialNum()));
        causes(BasicAddEvent.class,this);
    }

    public void totalMoney(){
        List<Fee> feeList=new Finder<>(Fee.class).byField("memberId",this.getId()).list(Sort.ASC("feeDate"));
        double totalMoney=0;
        for(Fee fee:feeList){
            totalMoney+=fee.getMoney();
        }
        if(feeList.size()>0){
            setFeeName(feeList.get(feeList.size()-1).getName());
            setFeeDate(feeList.get(feeList.size()-1).getFeeDate());
        }
        this.setMoney(totalMoney);
        causes(BasicModifyEvent.class,this);
    }
    public void sumCourseHour(){
        List<CourseHour> list=new Finder<>(CourseHour.class).byField("memberId",this.getId()).list();
        double sumHour=0;
        for(CourseHour courseHour : list){
            sumHour+=courseHour.getHour();
        }
        setUsedHours(sumHour);
        causes(BasicModifyEvent.class,this);
    }

    public void updateAge() {
        String birth=this.getBirth();
        Integer[] monthNum={0};
        Integer[] ageNum={0};
        if(StringUtils.isEmpty(birth)) return;
        String showAge = Utils.getPatientAge(DateUtils.parseDate(birth),ageNum,monthNum);
        setShowAge(showAge);
        //if(monthNum[0]>11) ageNum[0]++;
        setAge(ageNum[0].toString());
        causes(BasicModifyEvent.class, this);
    }
    public void generatePadCode(){
        setPadCode(CommonUtils.getRandomString(6));
        causes(BasicModifyEvent.class,this);
    }
}
